<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable @change="getVendorList()" />
        </el-form-item>
        <!-- <el-form-item prop="name">
          <el-input v-model="queryParams.stationName" placeholder="房站名称" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <!-- <el-form-item prop="vendorName">
          <el-input v-model="queryParams.vendorName" placeholder="酒店名称" clearable />
          multiple
        </el-form-item> -->

        <el-form-item prop="vendorId">
          <el-select v-model="queryParams.vendorId" placeholder="酒店名称" clearable>
            <el-option v-for="item in vendors" :key="item.vendorId" :label="item.name" :value="item.vendorId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="deviceType">
          <DictSelect dict="vendor_device_type" v-model="queryParams.deviceType" placeholder="设备类型" clearable />
        </el-form-item>
        <el-form-item prop="status">
          <DictSelect dict="device_status" v-model="queryParams.status" placeholder="状态" clearable />
        </el-form-item>
        <el-form-item prop="deviceNo">
          <el-input v-model="queryParams.searchValue" placeholder="机器名" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:device:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #type="{ row }">
          <dict-tag :options="vendor_device_type" :value="row.deviceType" />
        </template>
        <template #room="{ row }">
          <!-- <span>{{ row.room }}</span> -->
          <dict-tag :options="room_position" :value="row.roomPosition" />
        </template>
        <template #status="{ row }">
          <dict-tag :options="device_status" :value="row.status" />
        </template>
        <template #mode="{ row }">
          <el-switch :value="row.operationalMode=='1'" disabled> </el-switch>
        </template>
        <template #handle="{ row }">
          <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:device:query']">详情</el-button>
          <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
          <!-- <el-button
            link
            type="primary"
            :disabled="row.status == '301' || row.status == '302'"
            @click="handleChange(row)"
            v-hasPermi="['biz:device:edit']"
            >更换</el-button
          > -->
          <el-button
            link
            type="danger"
            :disabled="row.status == '301' || row.status == '302'"
            @click="handleInvalid(row)"
            v-hasPermi="['biz:device:edit']"
            >作废</el-button
          >
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>
  </div>
</template>

<script setup name="VendorDevice" lang="ts">
import { abolishDevice, changeDevice, listVendorDevice } from '@/api/device/index';
import { DeviceVo } from '@/api/device/types';
import { getTableConfig } from './columns';
import { listVendor, delVendor } from '@/api/vendor';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { vendor_device_type, device_status, room_position } = toRefs<any>(proxy?.useDict('vendor_device_type', 'device_status', 'room_position'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');
const vendors = ref([]);

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  deviceNo: '',
  stationName: '',
  deviceType: '',
  regionCode: '',
  status: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  let queryObj = queryParams.value;
  queryObj.diviceTypes = [queryObj.deviceType];
  const res = await listVendorDevice(queryObj);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 查询酒店列表 */
const getVendorList = async () => {
  const res = await listVendor({
    pageNum: 1,
    regionCode: queryParams.value.regionCode,
    pageSize: 10000
  });
  vendors.value = res.rows;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 更换按钮操作 */
const handleChange = async (row?: DeviceVo) => {
  const _ids = row?.deviceId || '';
  await proxy?.$modal.confirm('是否确认将"' + row?.deviceId + '"标记为更换状态？');
  loading.value = true;
  await changeDevice(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('操作成功');
};
/** 作废按钮操作 */
const handleInvalid = async (row?: DeviceVo) => {
  const _ids = row?.deviceId || '';
  await proxy?.$modal.confirm('是否确认将"' + row?.deviceId + '"标记为作废状态？');
  loading.value = true;
  await abolishDevice(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('操作成功');
};

/** 详情按钮操作 */
const handleDetail = async (row?: DeviceVo) => {
  proxy?.$tab.openPage('/device/vendorDeviceDetail', '', { id: row?.deviceId });
};

const handleEdit = async (row?: DeviceVo) => {
  //proxy?.$tab.openPage('/device/vendorDeviceDetail', '', { id: row?.deviceId });
};

onMounted(() => {
  getList();
  getVendorList();
});
</script>
